<?php
//17 接口

namespace Lib;

class Sc17interface
{
    const FN_GUNFO_F17 = "uinfo_url";    //得到用户信息
    const FN_GORDER_F17 = "order_url";    //得到用户信息
	//测试
    //const FN_APPKEY_F17 = "Baozou";    //得到用户信息
    //const FN_SERCKEY_F17 = "qu3iZPiQLPy3";    //得到用户信息
    const FN_APPKEY_F17 = "HbsGrading";    //得到用户信息
    const FN_SERCKEY_F17 = "FVUOjBD7R9JQ";    //得到用户信息

	


    protected  $_need_check = array(
	);	
	   //开发环境信息
    private static $InfoDev = array(
		'17_app_key' => 'Baozou', //17 公钥 
		'17_app_secretKey' => 'qu3iZPiQLPy3', //17 密钥 
		'uinfo_url' => 'http://api.test.17zuoye.net/v1/user/get.api', //接口 
		'order_url' => 'http://api.test.17zuoye.net/v2/order/register.api',
        'confirm_order_url' => 'http://www.test.17zuoye.net/apps/order/v2/mobile/confirm.vpage',//确认订单页
    );

	//测试环境信息
    private static $InfoTest = array(
		'17_app_key' => 'Baozou', //17 公钥 
		'17_app_secretKey' => 'qu3iZPiQLPy3', //17 密钥 
		'uinfo_url' => 'http://api.test.17zuoye.com/v1/user/get.api', //接口 
		'order_url' => 'http://api.test.17zuoye.com/v2/order/register.api',
        'confirm_order_url' => 'http://www.test.17zuoye.net/apps/order/v2/mobile/confirm.vpage',//确认订单页
    );
	
    //正式环境信息
    private static $InfoPro = array(
		'17_app_key' => 'Baozou', //17 公钥 
		'17_app_secretKey' => 'qu3iZPiQLPy3', //17 密钥 
		'uinfo_url' => 'https://api.17zuoye.com/v1/user/get.api', //接口 
		'order_url' => 'https://api.17zuoye.com/v2/order/register',
        'confirm_order_url' => 'https://www.17zuoye.com/apps/order/v2/mobile/confirm.vpage',//确认订单页
    );

    //大数据量的接口时需要设置超时秒数(默认为10秒)
    private static $timeOut = 1000;
    public static function setTimeOut($s)
    {
        self::$timeOut = (int)$s;
    }

    //获取当前环境的信息
    public static $info;
    public static function getInfo()
    {
        //根据当前环境获取接口信息
        if(!isset(self::$info)) {
			
			$env_name = trim(strtolower($_SERVER['ENV_NAME']));
			if(!empty($env_name) && $env_name == 'product')
			{
				self::$info = self::$InfoPro;
			}
			else if(!empty($env_name) && $env_name == 'test')
			{
				self::$info = self::$InfoTest;
			}
			else
			{
				self::$info = self::$InfoDev;
			}
        }

        return self::$info;
    }

    //以post方式调用api
    public static function api($fname=self::FN_GUNFO_F17,$data = array(),$show_code = false)
    {
		$result = array('result'=>false,'data'=>array(),'msg'=>'');
        //根据当前环境获取接口信息
        $info = self::getInfo();    
        $info['url'] = $info[$fname];
		//var_dump($info);
        $content = self::post($info['url'], $data,$show_code);

		$redata = json_decode($content,true);
		//var_dump($redata,$data); exit;
        if(!empty($redata) && $redata['result']=="success")
		{
			$result['result'] = true;
			$result['data']= $redata;
			$result['msg'] = 'ok';
        }
		else
		{
			$result['data']= $redata;
			$result['msg'] = isset($redata['error_info']) ? $redata['error_info'] : $redata['error_log'];
        }
		return $result;
    }

    //post提交数据
    public static function post($url,$data = array(), $show_code = false)
    {
		//$headers = array("Content-type: application/json;charset='utf-8'","Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache",);
		$curl = curl_init(); //初始化CURL句柄
        curl_setopt($curl, CURLOPT_URL, $url); //设置请求的URL
		//curl_setopt($curl, CURLOPT_HTTPHEADER , $header);
		curl_setopt($ch,CURLOPT_HTTPHEADER,array( "Content-Type: application/x-www-form-urlencoded;charset=utf-8"));
        //设为TRUE把curl_exec()结果转化为字串，而不是直接输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST,'POST');
		curl_setopt($curl,CURLOPT_ENCODING,'');
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//不检测服务器的证书是否由正规浏览器认证过的授权CA颁发

		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//不检测服务器的域名与证书上的是否一致
        //curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($data)); //设置POST提交的字符串
		curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,self::$timeOut); //设置连接超时的秒数

        $content = curl_exec($curl); //执行预定义的CURL


		//var_dump($url,$content,$data,$httpCode);		//exit;


		if(!empty($_GET['dbgs'])&&$_GET['dbgs']=='smyt')
		{
			var_dump($url,$content,$data);
			exit;
		}
        if ( $show_code ) {
            $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            curl_close($curl); //释放curl句

            return array(
                'content' => $content,
                'http_code' => $httpCode
            );
        }
        curl_close($curl); //释放curl句

        return $content;
    }

    //get提交
    public static function get($url,$data = array())
    {
        $curl = curl_init(); //初始化CURL句柄

        if(is_array($data) && !empty($data)){
            $data = trim(http_build_query($data));
        }
        !empty($data) && $url = "$url?$data";
        //echo $url;
        curl_setopt($curl, CURLOPT_URL, $url); //设置请求的URL
        //设为TRUE把curl_exec()结果转化为字串，而不是直接输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPGET, 1);//启用GET提交
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,self::$timeOut); //设置连接超时的秒数

        $content = curl_exec($curl); //执行预定义的CURL
        curl_close($curl); //释放curl句柄

        return $content;
    }

	public static function makeSig($params) {
		$info = self::getInfo();
		ksort($params);

		$queryString = array();
		foreach ($params as $key => $val ) {
			array_push($queryString,$key.'='.$val);
		}
		$sig=join('&', $queryString);

		$sig=md5($sig.$info['17_app_secretKey']);
		return $sig;
	}

    public static function signCheck($sessionkey)
    {
        $info = self::getInfo();    
		$str = "app_key=".$info['17_app_key']."&session_key=".$sessionkey.$info['17_app_secretKey'];
		return md5($str);
    }

	//得到公钥appkey
    public static function getkey()
    {
        $info = self::getInfo();    
		return $info['17_app_key'];
    }

    private static function _log($bizcode,$request,$response)
    {
        
    }

   
}

?>